(defparameter *全角标点符号* '(#\～ #\！ #\@ #\＃ #\￥ #\% #\… #\& #\× #\（ #\） #\— #\+ #\{ #\} #\| #\： #\“ #\” #\《  #\》 #\？ #\` #\- #\= #\· #\「  #\」 #\、 #\； #\‘ #\’ #\， #\。 #\/ #\ideographic_space))
(defparameter *半角标点符号* '(#\~ #\! #\@ #\# #\$ #\% #\^ #\& #\* #\( #\) #\_ #\+ #\{ #\} #\|  #\: #\" #\" #\< #\> #\? #\` #\- #\= #\[ #\] #\] #\\ #\; #\' #\' #\, #\. #\/ #\space))
;;句号在转化时由于语义定义需要改成感叹号,全角空格在转换为半角空格时变为两个.(仅限于处理cyl文件)
(defparameter *cyl半角标点符号* '(#\~ #\! #\@ #\# #\$ #\% #\^ #\& #\* #\( #\) #\_ #\+ #\{ #\} #\|  #\: #\" #\" #\< #\> #\? #\` #\- #\= #\[ #\] #\] #\\ #\; #\' #\' #\, #\! #\/ #\space))
(defparameter 中文命令 '(("显示目录"  "ls -l")("当前目录" "pwd")))


(defun change-half(x)(if (member x *全角标点符号*)  (elt  *半角标点符号*  (position x *全角标点符号*) ) x))
(defun change-cyl-half(x)(if (member x *全角标点符号*)  (elt  *cyl半角标点符号*  (position x *全角标点符号*) ) x))
(defun pr-quanjiao(&optional x) (if (null x) (let ((i 0)(d *半角标点符号*)) (dolist (c *全角标点符号*) (format t "[~a](~a)[~a]===[~a][~a]~%" (setf i (1+ i))  c (char-code c) (nth(1- i) d) (char-code (nth (1- i) d)))))(format t "[~a]---(~a)~%" x (char-code x))))

(defun string-half(x)(dotimes (i (length x))(setf (char x i) (change-half (char x i))))x)
(defun string-cyl-half(x)
  (let ((str nil)) 
    (dotimes (i (length x))
      (cond ((char= (char x i) #\ideographic_space) (setf str (concatenate 'string str "  ")))
            ((or (char= (char x i) #\:)(char= (char x i) #\：))                (setf str (concatenate 'string str ": ")))
            (t 	  (setf str (concatenate 'string str (string (change-cyl-half (char x i))))))))
    str))
(define (read-cyl-into-string name)
    (with-open-file (fp name)
      (let ((str nil)(s (read-line fp nil)))
	(loop (when (null s) (return-from read-cyl-into-string str))
	   (setf s (string-cyl-half s))
	   ;(print str)
	   (setf str (concatenate 'string str s (string #\newline)))
	   (setf s (read-line fp nil))))))
(define (read-into-string name &optioal flag)
    (with-open-file (fp name)
      (let ((str nil)(s (read-line fp nil)))
	(loop (when (null s) (return-from read-into-string str))
	   (if (null flag) (setf s (string-half s)))
	   ;(print str)
	   (setf str (concatenate 'string str s (string #\newline)))
	   (setf s (read-line fp nil))))))
(define (write-from-string s name)
    (with-open-file (fw name :direction :output :if-exists :supersede)
     (travel s 'for-index i
      (write-char (char s i) fw))))
(define (file-full-half fn)
    (write-from-string (read-into-string fn) "temp")
  (sh (format nil "mv temp ~a" fn)))

(defun bb-proc (str)
  (if (member str 中文命令 :key #'car :test #'string=)
      (shell (cadr (elt 中文命令 (position str 中文命令 :key #'car :test #'string=))))
      (prin1 (eval (read-from-string (cn-funcall str)))))
  (format t "~%"))


(defun all-nil-list(l)
  (let ((flag nil))
    (dolist (i l) (if (null i) (setf flag t) (return (setf flag nil))))
    flag))
;;中文y表达式转化函数c-call

(defun cn-funcall(s &optional (n 0) (lsign-count 0)(rsign-count 0) (deli "[]"))
  (let ((len (length s))(flag nil)(fun-str nil)(par-str nil)(par-list nil)(res-list nil)(sign-count 0))
      (loop for i from n to (1- (length s)) do
	   (let ((si (char s i)))
	  
	   (cond  ((and (null flag) (char= si #\[))(progn (setf flag t)(setf lsign-count (1+ lsign-count))
					;(format t "flag=[~a] i=[~a]~%" flag i)
					(setf res-list (cn-funcall s (1+ i) lsign-count rsign-count deli))
					;(format t "res-list=[~a]~%" res-list)
					(if (atom res-list) (return-from cn-funcall s)
					    (progn
					      (setf lsign-count (nth 2 res-list))
					      (setf rsign-count (nth 3 res-list))
					      (setf flag nil)
					      ;(setf fun-str (concatenate 'string fun-str (string #\_)))
					      (if deli (setf fun-str (concatenate 'string fun-str deli)))
					      (setf par-str (car res-list))
					      (push par-str par-list)
					      (setf i (cadr res-list))
					;(format t "par-str=[~a] i=[~a]~%" par-str i)
					      (if (>= (1+ i) len) (return))))))
		  ((and (null flag) (char= si #\]))(progn ;(format t "fun-str=[~a]~%" fun-str)
				        (setf rsign-count (1+ rsign-count))
				
					    (if (<= rsign-count lsign-count)
						(if (null par-list) (return-from cn-funcall (list fun-str i lsign-count rsign-count))
						    (if (null fun-str)
							(progn ;(format t "par-str=(~a) i=(~a)~%" par-str i)
							  (setf res-list (list par-str i lsign-count rsign-count))
					                  ;(format t "res-list=(~a)~%" res-list)
							  (return-from cn-funcall  res-list))
							(if (all-nil-list par-list) fun-str
							    (let ((fun-res (concatenate 'string (string #\() fun-str (string #\space))))
							      (dolist (k (reverse par-list))
								(setf fun-res (concatenate 'string fun-res (string k) (string #\space))))
							      (setf fun-res (concatenate 'string fun-res (string #\))))
							      (return-from cn-funcall (list fun-res i lsign-count rsign-count))))))
						(progn (setf rsign-count (1- rsign-count)) (setf fun-str (concatenate 'string fun-str (string si))))
						)))
		
		  ((and (char= si #\")(not (and (> i 0) (char= (char s (1- i)) #\\))))
		   (if (null flag) (setf flag t) (setf flag nil))(setf fun-str (concatenate 'string fun-str (string si))))
		  (t    (setf fun-str (concatenate 'string fun-str (string si)))))))
      (setf sign-count (+ lsign-count rsign-count))
      ;(format t "(~a[)(~a])~%" lsign-count rsign-count)
      (if (< sign-count 2)
	  s
	  (if (= (mod sign-count 2) 0)
	      (if (all-nil-list par-list) fun-str
		  (let ((fun-res (concatenate 'string (string #\() fun-str (string #\space))))
		    (dolist (k (reverse par-list))
		      (setf fun-res (concatenate 'string fun-res (string k) (string #\space))))
		    (setf fun-res (concatenate 'string fun-res (string #\))))))
	      (format t "中括号[]不匹配。")))))
	
(defun c-call(l)
  (cond ((null l) nil)
	((atom l)(let ((s (format nil "~s" l)))(read-from-string (cn-funcall s))))
	((consp l)(cons (c-call (car l))(c-call (cdr l))))
	(t (format t "ERROR: can't get here."))))
